
// lrcDlg.cpp : implementation file
//

#include "stdafx.h"
#include "lrc.h"
#include "lrcDlg.h"
#include "afxdialogex.h"
#include "lrc_api.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CAboutDlg dialog used for App About

class CAboutDlg : public CDialogEx
{
public:
	CAboutDlg();

// Dialog Data
	enum { IDD = IDD_ABOUTBOX };

	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support

// Implementation
protected:
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialogEx(CAboutDlg::IDD)
{
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)
END_MESSAGE_MAP()


// ClrcDlg dialog




ClrcDlg::ClrcDlg(CWnd* pParent /*=NULL*/)
	: CDialogEx(ClrcDlg::IDD, pParent)
	, m_lrcFile(_T(""))
	, m_lrcData(_T(""))
	, m_time(_T(""))
{
	m_Timer = 0;
	m_ts = 0;
	m_lrcH = 0;
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void ClrcDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Text(pDX, IDC_EDIT1, m_lrcFile);
	DDX_Text(pDX, IDC_EDIT2, m_lrcData);
	DDX_Text(pDX, IDC_EDIT3, m_time);
}

BEGIN_MESSAGE_MAP(ClrcDlg, CDialogEx)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON1, &ClrcDlg::OnBnClickedButton1)
	ON_WM_TIMER()
END_MESSAGE_MAP()


// ClrcDlg message handlers

BOOL ClrcDlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		BOOL bNameValid;
		CString strAboutMenu;
		bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
		ASSERT(bNameValid);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon

	// TODO: Add extra initialization here

	return TRUE;  // return TRUE  unless you set the focus to a control
}

void ClrcDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialogEx::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void ClrcDlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialogEx::OnPaint();
	}
}

// The system calls this function to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR ClrcDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}


void ClrcDlg::OnOK()
{
	return;
//	lrc_free();
//	CDialogEx::OnOK();
}


void ClrcDlg::OnCancel()
{
	if(m_Timer)
	{
		KillTimer(m_Timer);
		m_Timer = 0;
	}

	lrc_free();
	m_lrcH = 0;

	CDialogEx::OnCancel();
}


void ClrcDlg::OnBnClickedButton1()
{
	if(m_Timer)
	{
		KillTimer(m_Timer);
		m_Timer = 0;
		lrc_free();
		m_lrcH = 0;
		m_ts = 0;
		m_time = m_lrcData = m_lrcFile = _T("");
		UpdateData(0);
	}

	CString strFilter = _T("lrc file (*.lrc)|*.lrc||");
	CFileDialog dlg(TRUE, NULL, NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, strFilter);

	if(IDOK == dlg.DoModal())
	{
		m_lrcFile = dlg.GetPathName();
		UpdateData(0);

		char fname[MAX_PATH];
		sprintf_s(fname, MAX_PATH, "%s", m_lrcFile.GetBuffer());
		if(0==lrc_init(fname, 0))
		{
			m_lrcH = 1;
			m_ts = 0;
			m_Timer = SetTimer(0x100, 1000, NULL);
			m_lrcData.Format(_T("[%02d:%02d]"), m_ts/60, m_ts%60);
			m_time = m_lrcData;
		}
		else
		{
			AfxMessageBox(_T("lrc_init error !"));
		}

		UpdateData(0);
	}
}


void ClrcDlg::OnTimer(UINT_PTR nIDEvent)
{
	// TODO: Add your message handler code here and/or call default
	if(m_Timer == nIDEvent)
	{
		unsigned char * lrcData;
		m_ts++;
		m_time.Format(_T("[%02d:%02d]"), m_ts/60, m_ts%60);
		UpdateData(0);
		int ret = lrc_get(m_ts, &lrcData);
		switch(ret)
		{
		case 0://no lrc at this time
			break;

		case 1://has lrc at this time, render it
			m_lrcData.Format(_T("[%02d:%02d]"), m_ts/60, m_ts%60);
			m_lrcData += Utf8ToWideChar((char*)lrcData);
			UpdateData(0);
			break;

		case -1://play over, lrc free and kill timer
			if(m_Timer)
			{
				KillTimer(m_Timer);
				m_Timer = 0;
			}
			lrc_free();
			m_lrcH = 0;
			break;

		case -2://fs error
			ASSERT(0);
			break;

		default:
			ASSERT(0);//not arrived
			break;
		}
	}

	CDialogEx::OnTimer(nIDEvent);
}

CString ClrcDlg::Utf8ToWideChar(char * txt)
{
	wchar_t widestr[128];
	MultiByteToWideChar(CP_UTF8, 0, txt, -1, widestr, 128);
	return CString(widestr);
}
